package com.zhy.http.okhttp.cookie;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Log;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class PersistentCookieStore
  implements CookieStore
{
  private static final String COOKIE_NAME_PREFIX = "cookie_";
  private static final String COOKIE_PREFS = "CookiePrefsFile";
  private static final String LOG_TAG = "PersistentCookieStore";
  private final SharedPreferences cookiePrefs;
  private final HashMap<String, ConcurrentHashMap<String, HttpCookie>> cookies;

  public PersistentCookieStore(Context paramContext)
  {
    this.cookiePrefs = paramContext.getSharedPreferences("CookiePrefsFile", 0);
    this.cookies = new HashMap();
    Iterator localIterator = this.cookiePrefs.getAll().entrySet().iterator();
    while (true)
    {
      if (!localIterator.hasNext())
        return;
      Map.Entry localEntry = (Map.Entry)localIterator.next();
      if (((String)localEntry.getValue() != null) && (!((String)localEntry.getValue()).startsWith("cookie_")))
        for (String str1 : TextUtils.split((String)localEntry.getValue(), ","))
        {
          String str2 = this.cookiePrefs.getString("cookie_" + str1, null);
          if (str2 != null)
          {
            HttpCookie localHttpCookie = decodeCookie(str2);
            if (localHttpCookie != null)
            {
              if (!this.cookies.containsKey(localEntry.getKey()))
                this.cookies.put((String)localEntry.getKey(), new ConcurrentHashMap());
              ((ConcurrentHashMap)this.cookies.get(localEntry.getKey())).put(str1, localHttpCookie);
            }
          }
        }
    }
  }

  public void add(URI paramURI, HttpCookie paramHttpCookie)
  {
    String str = getCookieToken(paramURI, paramHttpCookie);
    if (!paramHttpCookie.hasExpired())
    {
      if (!this.cookies.containsKey(paramURI.getHost()))
        this.cookies.put(paramURI.getHost(), new ConcurrentHashMap());
      ((ConcurrentHashMap)this.cookies.get(paramURI.getHost())).put(str, paramHttpCookie);
    }
    while (true)
    {
      SharedPreferences.Editor localEditor = this.cookiePrefs.edit();
      localEditor.putString(paramURI.getHost(), TextUtils.join(",", ((ConcurrentHashMap)this.cookies.get(paramURI.getHost())).keySet()));
      localEditor.putString("cookie_" + str, encodeCookie(new SerializableHttpCookie(paramHttpCookie)));
      localEditor.commit();
      return;
      if (this.cookies.containsKey(paramURI.toString()))
        ((ConcurrentHashMap)this.cookies.get(paramURI.getHost())).remove(str);
    }
  }

  protected String byteArrayToHexString(byte[] paramArrayOfByte)
  {
    StringBuilder localStringBuilder = new StringBuilder(2 * paramArrayOfByte.length);
    int i = paramArrayOfByte.length;
    for (int j = 0; ; j++)
    {
      if (j >= i)
        return localStringBuilder.toString().toUpperCase(Locale.US);
      int k = 0xFF & paramArrayOfByte[j];
      if (k < 16)
        localStringBuilder.append('0');
      localStringBuilder.append(Integer.toHexString(k));
    }
  }

  protected HttpCookie decodeCookie(String paramString)
  {
    ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(hexStringToByteArray(paramString));
    try
    {
      HttpCookie localHttpCookie = ((SerializableHttpCookie)new ObjectInputStream(localByteArrayInputStream).readObject()).getCookie();
      return localHttpCookie;
    }
    catch (IOException localIOException)
    {
      Log.d("PersistentCookieStore", "IOException in decodeCookie", localIOException);
      return null;
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      Log.d("PersistentCookieStore", "ClassNotFoundException in decodeCookie", localClassNotFoundException);
    }
    return null;
  }

  protected String encodeCookie(SerializableHttpCookie paramSerializableHttpCookie)
  {
    if (paramSerializableHttpCookie == null)
      return null;
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
      new ObjectOutputStream(localByteArrayOutputStream).writeObject(paramSerializableHttpCookie);
      return byteArrayToHexString(localByteArrayOutputStream.toByteArray());
    }
    catch (IOException localIOException)
    {
      Log.d("PersistentCookieStore", "IOException in encodeCookie", localIOException);
    }
    return null;
  }

  public List<HttpCookie> get(URI paramURI)
  {
    ArrayList localArrayList = new ArrayList();
    if (this.cookies.containsKey(paramURI.getHost()))
      localArrayList.addAll(((ConcurrentHashMap)this.cookies.get(paramURI.getHost())).values());
    return localArrayList;
  }

  protected String getCookieToken(URI paramURI, HttpCookie paramHttpCookie)
  {
    return paramHttpCookie.getName() + paramHttpCookie.getDomain();
  }

  public List<HttpCookie> getCookies()
  {
    ArrayList localArrayList = new ArrayList();
    Iterator localIterator = this.cookies.keySet().iterator();
    while (true)
    {
      if (!localIterator.hasNext())
        return localArrayList;
      String str = (String)localIterator.next();
      localArrayList.addAll(((ConcurrentHashMap)this.cookies.get(str)).values());
    }
  }

  public List<URI> getURIs()
  {
    ArrayList localArrayList = new ArrayList();
    Iterator localIterator = this.cookies.keySet().iterator();
    while (true)
    {
      if (!localIterator.hasNext())
        return localArrayList;
      String str = (String)localIterator.next();
      try
      {
        localArrayList.add(new URI(str));
      }
      catch (URISyntaxException localURISyntaxException)
      {
        localURISyntaxException.printStackTrace();
      }
    }
  }

  protected byte[] hexStringToByteArray(String paramString)
  {
    int i = paramString.length();
    byte[] arrayOfByte = new byte[i / 2];
    for (int j = 0; ; j += 2)
    {
      if (j >= i)
        return arrayOfByte;
      arrayOfByte[(j / 2)] = ((byte)((Character.digit(paramString.charAt(j), 16) << 4) + Character.digit(paramString.charAt(j + 1), 16)));
    }
  }

  public boolean remove(URI paramURI, HttpCookie paramHttpCookie)
  {
    String str = getCookieToken(paramURI, paramHttpCookie);
    if ((this.cookies.containsKey(paramURI.getHost())) && (((ConcurrentHashMap)this.cookies.get(paramURI.getHost())).containsKey(str)))
    {
      ((ConcurrentHashMap)this.cookies.get(paramURI.getHost())).remove(str);
      SharedPreferences.Editor localEditor = this.cookiePrefs.edit();
      if (this.cookiePrefs.contains("cookie_" + str))
        localEditor.remove("cookie_" + str);
      localEditor.putString(paramURI.getHost(), TextUtils.join(",", ((ConcurrentHashMap)this.cookies.get(paramURI.getHost())).keySet()));
      localEditor.commit();
      return true;
    }
    return false;
  }

  public boolean removeAll()
  {
    SharedPreferences.Editor localEditor = this.cookiePrefs.edit();
    localEditor.clear();
    localEditor.commit();
    this.cookies.clear();
    return true;
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.zhy.http.okhttp.cookie.PersistentCookieStore
 * JD-Core Version:    0.6.2
 */